package ServerStuff;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.sql.Date;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONException;
import org.json.JSONObject;

import Database.*;
import DataStorageClasses.*;

@WebServlet("/UpdateEmployeeServlet")
public class UpdateEmployeeServlet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	long empID;
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		DatabaseConnector dbConnect = new DatabaseConnector();
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		HttpSession session = req.getSession();
		
		//System.out.println(session.getAttribute("id"));
		//session.setAttribute("id", 2);
		//System.out.println(session.getAttribute("id"));
		String busName = (String) session.getAttribute("username");
		String employeeInfo = (String) req.getParameter("employee");
		
		String delims = "[ ]+";
		
		String[] info = employeeInfo.split(delims);
		
		empID = Long.parseLong(info[0]);
		Employee emp = dbConnect.getEmployee(empID, busName);
	
		JSONObject result = new JSONObject();
		
		
		try{
		//Integer employeeID = (Integer) session.getAttribute("id");
			
		//long empID = employeeID.longValue();
		//long empID
		
		try {
			result.put("employeeFirstName", emp.getFirstName());
			result.put("employeeLastName", emp.getLastName());
			result.put("empID", emp.getEmployeeID());
			result.put("employer", emp.getBusinessName());
			result.put("telephone", emp.getHomeTel());
			result.put("cellPhone", emp.getCell());
			result.put("dateHired", emp.getDateHired());
			result.put("dateFired", emp.getDateFired());
			result.put("wage", emp.getWage());
			result.put("SIN", emp.getSIN());
			result.put("address", emp.getStreet());
			result.put("city", emp.getCity());
			result.put("province", emp.getProvince());
			result.put("postalCode",emp.getPostalCode());
			result.put("employeeID",emp.getEmployeeID());
			
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		}
		catch (NumberFormatException e){
			
			e.printStackTrace();
		}
		
		res.getWriter().print(result);
		res.setStatus(HttpURLConnection.HTTP_ACCEPTED);
	
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
		DatabaseConnector dbConnect = new DatabaseConnector();
		
		HttpServletRequest req = (HttpServletRequest) request;
		//HttpServletResponse res = (HttpServletResponse) response;
		//HttpSession session = req.getSession();
		
		//String busName = (String) session.getAttribute("username");
	
		//long empID = (Long) session.getAttribute("id") ;
		
		//Employee emp = dbConnect.getEmployee(empID, busName);
		
		
    	String newFirstName = req.getParameter("firstName");
    	String newLastName = req.getParameter("lastName");
    	
    	int yearHired = Integer.parseInt(req.getParameter("dateHiredYear"));
    	int monthHired = Integer.parseInt(req.getParameter("dateHiredMonth"));
    	int dayHired = Integer.parseInt(req.getParameter("dateHiredDay"));
    	
    	//String startDate = req.getParameter("dateHiredYear")+req.getParameter("dateHiredMonth")+ req.getParameter("dateHiredDay");
    	
    	Calendar calendarDateHired = Calendar.getInstance();
    	calendarDateHired.set(yearHired, monthHired, dayHired);
    	Date dateHired = (Date) calendarDateHired.getTime();
    	
    	
    	int endYear = Integer.parseInt(req.getParameter("endYear"));
    	int endMonth = Integer.parseInt(req.getParameter("endMonth"));
    	int endDay = Integer.parseInt(req.getParameter("endDay"));
    	
    	//String endDate = req.getParameter("endYear")+req.getParameter("endMonth")+ req.getParameter("endDay");
    	
    	Calendar calendarDateFired = Calendar.getInstance();
    	calendarDateFired.set(endYear, endMonth, endDay);
    	Date dateFired = (Date) calendarDateFired.getTime();
    	
    	double wage = Double.parseDouble(req.getParameter("wage"));
    	long SIN = Long.parseLong(req.getParameter("SIN"));
    	String address = req.getParameter("address");
    	String city = req.getParameter("city");
    	String province = req.getParameter("province");
    	String postalCode = req.getParameter("postalCode");
    	String telephone = req.getParameter("telephone");
    	String cell = req.getParameter("cell");
    	
    	
    	dbConnect.updateEmployee(empID, newFirstName, newLastName, dateHired, dateFired, wage, SIN, address, city, province,
    			postalCode, telephone, cell);
    	//System.out.println(newFirstName);
	}

}
